package actions;

import gui.Frame;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;

/**
 * An action for sorting jobs by specific criteria.
 * User story 7
 * @author Christian
 *
 */
@SuppressWarnings("serial")
public class SortJobsAction extends AbstractAction {
	private final Frame myFrame;
	private final List<String> mySortCriteria;
	
	/**
	 * Takes the newest sort criteria and adds it to the collection of sort criterion as the newest.
	 * This was supposed to perform as a primary, secondary, tertiary sort, but I couldn't get it to work.
	 * It only sorts by most recent.
	 * The criterion available are Region, District, Park, and Date
	 * @param theFrame
	 * @param theNewestSort The newest sort criteria
	 * @param theSortCriteria The collection of sort criterion
	 */
	public SortJobsAction(final Frame theFrame, final String theNewestSort, List<String> theSortCriteria) {
		super(theNewestSort);
		if (theSortCriteria == null) {
			theSortCriteria = new ArrayList<String>();
		} else if (theSortCriteria.contains(theNewestSort)) {
			//removes the criteria
			theSortCriteria.remove(theSortCriteria.indexOf(theNewestSort));
		}
		//put the newest criteria at the head of the list
		theSortCriteria.add(0, theNewestSort);
		if (theSortCriteria.size() > 4) {
			//remove the oldest if > 4
			theSortCriteria.remove(4);
		}
		myFrame = theFrame;
		mySortCriteria = new ArrayList<String>(theSortCriteria);
//		mySortCriteria = theSortCriteria;
	}
	
	/**
	 * Sets the previous action for the last screen displayed.
	 * Starts browsing of jobs in order sorted by criterion passed in.
	 */
	public void actionPerformed(final ActionEvent theEvent) {
		myFrame.setPreviousAction(new HomeAction(myFrame));
		myFrame.browseJobs(mySortCriteria);
//		myFrame.displaySuccess("criteria: "+mySortCriteria);
	}
}
